//
//  HomeContentCell.m
//  NormalFramework
//
//  Created by beisheng03 on 2018/3/13.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "HomeContentCell.h"
#import "HomeCollectionVC.h"
#import "HomeCollecLayout.h"

@interface HomeContentCell ()
@property (nonatomic, strong) NSArray<NSString *> *titles;

/** <#note#> */
@property (nonatomic, strong) NSMutableArray<HomeCollectionVC *> *contentVCs;

@property (nonatomic, weak) UIScrollView *contentScrollView;
@end

@implementation HomeContentCell

- (NSMutableArray<HomeCollectionVC *> *)contentVCs{
    
    if (!_contentVCs) {
        _contentVCs = [[NSMutableArray alloc]init];
    }
    
    return _contentVCs;
}


- (NSArray<NSString *> *)titles
{
    if (!_titles) {
        _titles = @[@"人气",@"最新",@"进度",@"总需人次"];
    }
    return _titles;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.contentView.backgroundColor = [UIColor blueColor];
    
    [self setupAllChildViewController];
    
    [self setupContentScrollView];
    // Initialization code
}



#pragma mark -添加子控制器
- (void)setupAllChildViewController
{
    
    for (NSString *title in self.titles) {
        
        HomeCollectionVC *vc = [[HomeCollectionVC alloc]initWithCollectionViewLayout:[[HomeCollecLayout alloc]init]];
        vc.title = title;
        [self.contentVCs addObject:vc];
    }
    
  
}



#pragma mark - 搭建内容滚动视图
- (void)setupContentScrollView
{
    // 创建UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    //    CGFloat y = CGRectGetMaxY(_titleScrollView.frame);
    //    CGFloat h = XMGScreenH - y;
    //    CGFloat w = XMGScreenW;
    //    scrollView.frame = CGRectMake(0, y, w, h);
    
    scrollView.frame = CGRectMake(0, 0, KScreenW, KScreenH - (IPhoneX ? 88 : 64));
    scrollView.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:scrollView];
    self.contentScrollView = scrollView;
    
    
    
    
    // 设置内容滚动范围
    self.contentScrollView.contentSize = CGSizeMake(4 * KScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    
    
    
    /*
     问题:1.按钮文字为黑色 2.标题滚动视图可以滚动
     */
    [self titleClick:nil];
    

    
}




#pragma mark -点击标题就会调用
- (void)titleClick:(UIButton *)button
{
    
    // 2.需要把对应子控制器的view添加上去
    [self setupOneViewControllerView:0];
    
    // 3.设置内容滚动视图偏移量
    // 计算偏移量
    CGFloat offsetX = 0;
    self.contentScrollView.contentOffset = CGPointMake(offsetX, 0);
}







// 添加一个子控制器的view
- (void)setupOneViewControllerView:(NSInteger)i
{
    
    HomeCollectionVC *vc = self.contentVCs[0];
    if (vc.view.superview) return;
    
    //    CGFloat navH = IPhoneX ? 88 : 64;
    vc.view.frame = CGRectMake(0, 0, KScreenW, self.contentScrollView.bounds.size.height);
    [self.contentScrollView addSubview:vc.view];
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
